{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0ae7890921ac3c17143ff000ac7152addc6614e2051824da39aa37dab63d26d82",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'lisi', 'zs'}"
      ]
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "# 掌握5大金刚之string，和9字口诀：基建-增删改查操-切复\n",
    "#5大金刚包括：list, tuple, str, set集合, dict\n",
    "\n",
    "#1. 基础：不能重复，不分先后，不收容可变的东西，只能加入immutable（不可变）的内容\n",
    "#什么情况下用set——不能重复，不分先后的时候用\n",
    "name = {'zs','lisi','zs'}\n",
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{4, 5, 6}"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "#2.创建-空的{}默认指的是dict字典，用set可以空的，也可以转化iterable的对象\n",
    "score = set()\n",
    "score = {98,69,86,72}\n",
    "score = set([4,5,6])#能够枚举的都可以被转化成set iterable\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{4, 5, 6, 'zs'}"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "#3. 增加\n",
    "score.add('zs')#append是指在后面加，set不需要先来后到，直接用addv\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{4, 5, 6}"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "#4. 删\n",
    "score.remove('zs')\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "# score.discard('ls')#有就移除，没有就什么也不做也不会报错\n",
    "score.clear()#清楚所有内容\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "#5. 改——没有顺序，所以没有下标，没办法改\n",
    "#score.add(88)#报错，因为不可改了\n",
    "#6. 查 in,not in 成员运算符\n",
    "score = {98,67,86}\n",
    "98 not in score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7, 8}"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "#7.操作：set集合——并，交\n",
    "s1 = {1,2,3,4,5}\n",
    "s2 = {4,5,6,7,8}\n",
    "#合并\n",
    "# s3 = s1|s2\n",
    "s3 = s1.union(s2)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{4, 5}"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "#交\n",
    "s3 = s1 & s2 #s1.intersection(s2)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "#差\n",
    "s3 = s1 - s2 #在s1但不在s2\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "False\n"
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{1, 2, 3, 6, 7, 8}"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "#取不重复的并集\n",
    "s3 = s1^s2 \n",
    "print(s1.issuperset(s2)) # s1 > s2 #判断s1是否都在s2里面\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{67, 86, 98}"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "# 切片——set是集合，无序，没有切片\n",
    "#复制\n",
    "s4 = score.copy()\n",
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "frozenset({67, 86, 98})"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "#补充 frozenset()\n",
    "s5 = frozenset(s4)\n",
    "s5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}